<?php
class bc_db
{
	public $sqldbtxt = array(), $sql, $type;
	private $row = array(), $dbdate, $db, $dbID, $dbHost, $dbUsername, $dbPassword, $dberr, $dbconn, $db_query, $k=0, $err, $dbQuery_txt, $m_n_rows, $m_f_array, $m_s_db, $sql_ret_txt;
	
	public function __construct($ID,$Host,$Username,$Password,$db) 
	{
		$this->db = $db;
		$this->dbID = $ID;
		$this->dbHost = $Host;
		$this->dbUsername = $Username;
		$this->dbPassword = $Password;
	}
	
	function dbConnect()
	{
		if (!$this->dbconn)
		{
			//echo $this->dbHost."|".$this->dbUsername."|".$this->dbPassword;
			switch($this->dbID)
			{
				case 1;
				$this->dbconn = mysql_connect($this->dbHost,$this->dbUsername,$this->dbPassword);
				break;
				case 2;break;
				default:break;
				
			}
		}
		return $this->dbconn;
	}
	
	function dbClose()
	{
		if (@mysql_close($this->dbconn))
		{
			switch($this->dbID)
			{
				case 1;
				@mysql_close($this->dbconn);break;
				case 2;break;
				default:break;
				
			}
		}
	}
	
	function dbselectdb()
	{
		if (!$this->dbconn)
		{
			$this->dbconn = $this->dbConnect();
			$this->dberr = mysql_select_db($this->db,$this->dbconn);
			$this->err = mysql_error(); 
			if($this->err)
			{
				$this->m_s_db = "Unable to access database testdb.".$this->err;
			}
		}
		return $this->m_s_db;
	}
	
	
	function dbQuery($sql)
	{
		switch($this->dbID)
		{
			case 1;
				$this->dbQuery_txt = mysql_query($sql);
				$this->err = mysql_error(); 
				if($this->err) $this->dbQuery_txt = false;
				break;
			case 2;
				$this->dbQuery_txt = false;
				break;
			default:
				$this->dbQuery_txt = false;
			break;
			
		}
		return $this->dbQuery_txt;
	}
	
	
	function dbsql($sql,$type)
	{
		switch($this->dbID)
		{
			case 1;
			{
				mysql_query("set names 'gbk'");
				$this->db_query = $this->dbQuery($sql);
				if ($this->db_query!=false)
				{
					while ($this->sql_ret_txt = @mysql_fetch_array($this->db_query,$type))
					{
						$row[] = $this->sql_ret_txt;
					}
					if (count($row)==0)
					{
						if (similar_text(strtoupper(substr($sql,0,10)),"INSERT")==6) $this->m_f_array = @mysql_insert_id($this->dbconn);
						else $this->m_f_array = false;
					}
					else
					{
						$this->m_f_array = $row;
					}
				}
				else
				{
					$this->m_f_array = $this->err;
				}
				break;
			}
			case 2;
				$this->m_f_array = false;
			break;
			default:
				$this->m_f_array = false;
			break;
			
		}
		return $this->m_f_array;
	}
	
	function dbNumrows($dataHandle)
	{
		switch($this->dbID)
		{
			case 1;
				$this->$m_n_rows = @mysql_num_rows($dataHandle);
			break;
			case 2;
				$this->$m_n_rows = false;
			break;
			default:
				$this->$m_n_rows = false;
			break;
		}
		return $this->$m_n_rows;
	}
	
	function setsql($sql,$type,$k=0)
	{	
		$this->dbselectdb();
		$this->sqldbtxt = $this->dbsql($sql,$type);
		if ($k==0)
		$this->dbClose();
		return $this->sqldbtxt;
	}
}
?>
